<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-dropdown>
        <a-button  type="primary" icon="plus">新增任务<a-icon type="down" /></a-button>
        <template #overlay>
          <a-menu>
            <a-menu-item @click="handleAddTask('0')">
               SQL流任务
            </a-menu-item>
            <a-menu-item @click="handleAddTask('2')">
               SQL批任务
            </a-menu-item>
            <a-menu-item @click="handleAddTask('1')">
               JAR任务
            </a-menu-item>
          </a-menu>
        </template>
      </a-dropdown>
      <!-- 高级查询区域 -->
      <j-super-query :fieldList="superFieldList" ref="superQueryModal" @handleSuperQuery="handleSuperQuery"></j-super-query>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel"><a-icon type="delete"/>删除</a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作 <a-icon type="down" /></a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>

      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        class="j-table-force-nowrap"
        @change="handleTableChange">

        <!--   列表样式  状态     -->
        <template slot="customStatus" slot-scope="text, record">
          <a-tag v-if="record.status===-2||record.status==='UNKNOWN'"  size="mini">{{ getStatusDesc(record.status) }}</a-tag>
          <a-tag v-else-if="record.status===-1||record.status==='FAIL'" color="red" size="mini">{{ getStatusDesc(record.status) }}</a-tag>
          <a-tag v-else-if="record.status===0||record.status==='STOP'" color="yellow" size="mini">{{ getStatusDesc(record.status) }}</a-tag>
          <a-tag v-else-if="record.status===1||record.status==='RUN'" color="blue" size="mini">{{ getStatusDesc(record.status) }}</a-tag>
          <a-tag v-else-if="record.status===2||record.status==='STARTING'" color="blue" size="mini">{{ getStatusDesc(record.status) }}</a-tag>
          <a-tag v-else-if="record.status===3||record.status==='SUCCESS'" color="green" size="mini">{{ getStatusDesc(record.status) }}</a-tag>
          <a-tag v-else type="eye" size="mini">{{ getStatusDesc(record.status) }}</a-tag>
        </template>



        <!--   列表样式  开关     -->
        <template slot="customJobType" slot-scope="text, record">
          <a-tag v-if="record.jobType===0" color="blue" size="mini">{{ getJobTypeDesc(record.jobType) }}</a-tag>
          <a-tag v-else-if="record.jobType===1" color="blue" size="mini">{{ getJobTypeDesc(record.jobType) }}</a-tag>
          <a-tag v-else-if="record.jobType===2" color="blue" size="mini">{{ getJobTypeDesc(record.jobType) }}</a-tag>
        </template>


        <span slot="savepoint" slot-scope="text, record">
          <span   v-if="record.status===1" style= "margin-right: 5px"><a-icon type="logout" /><a style= "color:grey" @click="handleStoreTask(record)">备份</a></span>
          <span    v-if="record.status!==1" style= "margin-right: 5px"><a-icon type="login" /><a  style="color:grey"  @click="handleRestoreTask(record)">恢复</a></span>
        </span>


        <span slot="action" slot-scope="text, record">
          <span  v-if="record.status!==1" style= "margin-right: 5px"><a-icon  type="play-circle" style="color:green"/><a style= "color:green" @click="handleEditTask(record)">启动</a></span>
          <span  v-if="record.status===1" style= "margin-right: 5px"><a-icon type="stop" style="color:red" /><a v-if="record.status===1" style="color:red"  @click="handleEditTask(record)">停止</a></span>
          <span  style= "margin-right: 5px"><a-icon type="info" style= "color:grey"/><a style= "color:grey"  @click="handleDetailTask(record)">查看</a></span>
          <span  v-if="record.status!==1" style= "margin-right: 5px"><a-icon style="color:#1890FF"type="edit" /><a  @click="handleEditTask(record)">编辑</a></span>
          <span  style= "margin-right: 5px"><a-icon style="color:#1890FF" type="copy" /><a @click="handleEditTask(record)">复制</a></span>
          <span  v-if="record.status!==1" style= "margin-right: 5px"><a-icon style= "color:red" type="delete" /><a  style= "color:red" @click="handleEditTask(record)">删除</a></span>
        </span>

      </a-table>
    </div>

    <job-config-modal ref="modalForm" @ok="modalFormOk" :currSelectedJobType="selectJobType"></job-config-modal>
    <job-savepoint-modal ref="restoreModal" @ok="modalFormOk"></job-savepoint-modal>
  </a-card>
</template>

<script>

  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import JobConfigModal from './modules/JobConfigModal.Style#Drawer.vue'
  import JobSavepointModal from './modules/JobSavepointModal.vue'
  import {savepoint} from '@/api/apiStreamPlat'

  export default {
    name: 'JobConfigList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      JobConfigModal,JobSavepointModal
    },
    data () {
      return {
        description: 'job_config管理页面',
        // 表头
        columns: [
          {
            title: '#',
            dataIndex: '',
            key:'rowIndex',
            width:60,
            align:"center",
            customRender:function (t,r,index) {
              return parseInt(index)+1;
            }
          },
          {
            title:'任务名称',
            align:"center",
            dataIndex: 'jobName'
          },
          {
            title:'任务类型',
            align:"center",
            dataIndex: 'jobType',
            scopedSlots: { customRender: 'customJobType' }
          },
          {
            title:'状态',
            align:"center",
            dataIndex: 'status',
            scopedSlots: { customRender: 'customStatus' }
          },
          {
            title:'运行模式',
            align:"center",
            dataIndex: 'deployMode'
          },
          {
            title:'运行后的任务id',
            align:"center",
            dataIndex: 'jobId'
          },
          {
            title:'调度时间',
            align:"center",
            dataIndex: 'cron'
          },

          {
            title:'最后一次启动时间',
            align:"center",
            dataIndex: 'lastStartTime'
          },
          {
            title: '保存状态',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'savepoint' }
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:147,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/dcloud-flink-stream-platform/api/jobConfig/v1/list",
        },
        dictOptions:{},
        superFieldList:[],
        selectJobType:'' //选中的任务类型
      }
    },
    created() {
    this.getSuperFieldList();
    },
    computed: {

    },
    methods: {
      initDictConfig(){
      },
      getSuperFieldList(){
        let fieldList=[];
        fieldList.push({type:'string',value:'jobName',text:'任务名称'})
        fieldList.push({type:'string',value:'jobDesc',text:'任务描述'})
        fieldList.push({type:'string',value:'deployMode',text:'提交模式: standalone 、yarn 、yarn-session '})
        fieldList.push({type:'string',value:'flinkRunConfig',text:'flink运行配置'})
        fieldList.push({type:'string',value:'flinkSql',text:'sql语句'})
        fieldList.push({type:'string',value:'flinkCheckpointConfig',text:'checkPoint配置'})
        fieldList.push({type:'string',value:'jobId',text:'运行后的任务id'})
        fieldList.push({type:'int',value:'isOpen',text:'1:开启 0: 关闭'})
        fieldList.push({type:'int',value:'status',text:'1:运行中 0: 停止中 -1:运行失败'})
        fieldList.push({type:'string',value:'cron',text:'批任务定时调度 如 0/20 * * * * ? 表示每20秒 执行任务 '})
        fieldList.push({type:'string',value:'extJarPath',text:'udf地址已经连接器jar 如http://xxx.xxx.com/flink-streaming-udf.jar'})
        fieldList.push({type:'datetime',value:'lastStartTime',text:'最后一次启动时间'})
        fieldList.push({type:'int',value:'lastRunLogId',text:'最后一次日志'})
        fieldList.push({type:'int',value:'version',text:'更新版本号 用于乐观锁'})
        fieldList.push({type:'int',value:'jobType',text:'任务类型 0:sql 1:自定义jar'})
        fieldList.push({type:'string',value:'customArgs',text:'启动jar可能需要使用的自定义参数'})
        fieldList.push({type:'string',value:'customMainClass',text:'程序入口类'})
        fieldList.push({type:'string',value:'customJarUrl',text:'自定义jar的http地址 如:http://ccblog.cn/xx.jar'})
        fieldList.push({type:'int',value:'isDeleted',text:'isDeleted'})
        fieldList.push({type:'datetime',value:'editTime',text:'修改时间'})
        fieldList.push({type:'string',value:'creator',text:'creator'})
        fieldList.push({type:'string',value:'editor',text:'editor'})
        this.superFieldList = fieldList
      },
      getStatusDesc(status) { // 任务状态
        switch (status) {
          case -2: return '未知'
          case -1: return '失败'
          case 0: return '停止'
          case 1: return '运行中'
          case 2: return '启动中'
          case 3: return '提交成功'
          case 'UNKNOWN': return '未知'
          case 'FAIL': return '失败'
          case 'STOP': return '停止'
          case 'RUN': return '运行中'
          case 'STARTING': return '启动中'
          case 'SUCCESS': return '提交成功'
          default: return ''
        }
      },
      handleAddTask(jobType){
        this.selectJobType = jobType
        this.handleAdd()
      },
      handleEditTask(record){
        this.selectJobType = record.jobType
        this.handleEdit(record)
      },
      handleDetailTask(record){
        this.selectJobType = record.jobType
        this.handleDetail(record)
      },
      handleRestoreTask(record){
        this.$refs.restoreModal.list(record);
      },
      handleStoreTask(record){
        this.$confirm({
          content: '确认是否备份？',
          onOk: () => {
            let param = {id:record.id}
            savepoint(param).then(res =>{
              if(res.success){
                this.$message.success(res.message)
              }else{
                this.$message.error(res.message)
              }
            }).catch(err =>{
              this.$message.error(err.message)
            })
          }
        })


      },
      getJobTypeDesc(jobType){
        switch (jobType) {
          case 0:
            return 'SQL_STREAM'
          case 1:
            return 'JAR'
          case 2:
            return 'SQL_BATCH'
          default: return ''
        }
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';

  .ant-switch-checked {
    background-color: #13ce66; /* 文本颜色 */
  }
</style>